use lib qw(../lib);

use strict;
use Test::More tests => 29;
use Sys::Hostname;
use POSIX qw(strftime);

# Test 1 -- use Stdlog::Formatter
BEGIN { use_ok('Stdlog::Formatter');}

my ($fmtr, $fmt, @tmp, $s, $t);


# Test 2
@tmp = Stdlog::Formatter::parse_format_str("foo/bar");
is( "@tmp", q["foo/bar"],
   "Parse static string, no directives" );

# Test 3
$fmtr = Stdlog::Formatter->new("foo/bar");
is( ref($fmtr), 'Stdlog::Formatter',
   "make_formatter returns a coderef" );

# Test 4
is( $fmtr->(), "foo/bar",
    "Run static string, no directives" );


# Test 5
@tmp = Stdlog::Formatter::parse_format_str('foo/$ENV{PATH}/bar');
is( "@tmp", q["foo/$ENV{PATH}/bar"],
   'Parse single interpolated ($ENV{PATH}) string' );

# Test 6
$fmtr = Stdlog::Formatter->new('foo/$ENV{PATH}/bar');
is( $fmtr->("message"), "foo/".$ENV{PATH}."/bar",
    'Run single interpolated ($ENV{PATH}) string' );


# Test 7
@tmp = Stdlog::Formatter::parse_format_str("foo/%{pid}");
is( "@tmp", q["foo/" $$],
   'Parse single scalar directive (%{pid})' );

# Test 8
$fmtr = Stdlog::Formatter->new("foo/%{pid}");
is( $fmtr->("message"), "foo/$$",
    'Run single scalar directive (%{pid})' );


# Test 9
@tmp = Stdlog::Formatter::parse_format_str("%{PATH:env}/bar");
is( "@tmp", q[$ENV{PATH} "/bar"],
   'Parse single scalar directive (${PATH:env}) with an argument' );

# Test 10
$fmtr = Stdlog::Formatter->new("%{PATH:env}/bar");
is( $fmtr->("message"), "$ENV{PATH}/bar",
    'Run single scalar directive (${PATH:env}) with an argument' );


# Test 11
@tmp = Stdlog::Formatter::parse_format_str("foo/%{filename}/bar");
is( "@tmp", q["foo/" $args->{filename} "/bar"],
    'Parse single coderef directive (%{filename})' );

# Test 12
$fmtr = Stdlog::Formatter->new("foo/%{filename}/bar");
is( $fmtr->({filename => __FILE__}, "message"), "foo/".__FILE__."/bar",
    'Run single coderef directive (%{filename})' );


# Test 13
@tmp = Stdlog::Formatter::parse_format_str("foo/%{pid}/mid/%{filename}/bar");
is( "@tmp", q["foo/" $$ "/mid/" $args->{filename} "/bar"],
    'Parse two directives (%{pid} and %{filename})' );

# Test 14
$fmtr = Stdlog::Formatter->new("foo/%{pid}/mid/%{filename}/bar");
is( $fmtr->({filename => __FILE__}, "message"), "foo/$$/mid/".__FILE__."/bar",
    'Run two directives (%{pid} and %{filename})' );


# Test 15
$fmt = $Stdlog::Formatter::tmsp_fmt;
@tmp = Stdlog::Formatter::parse_format_str("%{timestamp}");
is( "@tmp", qq[sf_gtime('$fmt', \$args->{time})],
    'Parse %{timestamp} directive' );

# Test 16
# n.b. Win32 fucks up the %z strftime directive, god I hate...
$fmtr = Stdlog::Formatter->new("pre %{timestamp} post");
$t = time;
$s = strftime($fmt, gmtime($t));
is( $fmtr->({'time' => $t}, "message"), "pre $s post",
    'Checking %{timestamp} directive' );


# Test 17
$fmt = $Stdlog::Formatter::tmsp_fmt;
@tmp = Stdlog::Formatter::parse_format_str("%{datetime}");
is( "@tmp", qq[sf_ltime('$fmt', \$args->{time})],
    'Parse %{datetime} directive' );

# Test 18
$fmtr = Stdlog::Formatter->new("pre %{datetime} post");
$t = time;
$s = strftime($fmt, localtime($t));
is( $fmtr->({'time' => $t}), "pre $s post",
    'Checking %{datetime} directive' );


# Test 19
$fmt = $Stdlog::Formatter::date_fmt;
@tmp = Stdlog::Formatter::parse_format_str("%{date}");
is( "@tmp", qq[sf_ltime('$fmt', \$args->{time})],
    'Parse %{date} directive' );

# Test 20
$fmtr = Stdlog::Formatter->new("pre %{date} post");
$t = time;
$s = strftime($fmt, localtime($t));
is( $fmtr->({'time' => time}), "pre $s post",
    'Checking %{date} directive' );


# Test 21
$fmt = $Stdlog::Formatter::time_fmt;
@tmp = Stdlog::Formatter::parse_format_str("%{time}");
is( "@tmp", qq[sf_ltime('$fmt', \$args->{time})],
    'Parse %{time} directive' );

# Test 22
$fmtr = Stdlog::Formatter->new("pre %{time} post");
$t = time;
$s = strftime($fmt, localtime($t));
is( $fmtr->({'time' => time}), "pre $s post",
    'Checking %{time} directive' );


# Test 23
@tmp = Stdlog::Formatter::parse_format_str("%{foo:opt}");
is( "@tmp", qq[\$args->{opt}{foo}],
    'Parse %{opt} directive' );

# Test 24
$fmtr = Stdlog::Formatter->new("pre %{foo:opt} post");
is( $fmtr->({'opt' => {foo => 'FOO'}}), "pre FOO post",
    'Checking %{foo:opt} directive' );


# Test 25
@tmp = Stdlog::Formatter::parse_format_str("%{host}");
is( "@tmp", qq[\$Stdlog::Formatter::fmt_dirs{host}->(\$args, '')],
    'Parse %{host} directive' );

# Test 26
$fmtr = Stdlog::Formatter->new("pre %{host} post");
is( $fmtr->({}), "pre ".hostname()." post",
    'Checking %{host} directive' );

# Test 27
@tmp = Stdlog::Formatter::parse_format_str("%{message}");
is( "@tmp", qq[\@_],
    'Parse %{message} directive' );

# Test 28
$fmtr = Stdlog::Formatter->new('pre %{message} post');
is( $fmtr->({}, "message"), "pre message post",
    'Checking %{message} directive' );

# Test 29
$fmtr = Stdlog::Formatter->new('');
is( $fmtr->({}, "foo"), "foo",
    'Empty format string defaults to message' );


## need tests for message
## need tests for \'s in format strings
